//
//  CryptoUtils.swift
//  Utility
//
//  Created by yuhus on 2023/6/9.
//

import Foundation
import CryptoKit

public enum CryptoUtils {
  public static func randomNonceString(length: Int = 32) -> String {
    precondition(length > 0)
    var randomBytes = [UInt8](repeating: 0, count: length)
    let errorCode = SecRandomCopyBytes(kSecRandomDefault, randomBytes.count, &randomBytes)
    if errorCode != errSecSuccess {
      fatalError(
        "Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)"
      )
    }
    
    let charset: [Character] =
    Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
    
    let nonce = randomBytes.map { byte in
      // Pick a random character from the set, wrapping around if needed.
      charset[Int(byte) % charset.count]
    }
    
    return String(nonce)
  }
  
  @available(iOS 13, *)
  public static func SHA256(_ input: String) -> String {
    let inputData = Data(input.utf8)
    let hashedData = CryptoKit.SHA256.hash(data: inputData)
    
    let hashString = hashedData
      .compactMap { String(format: "%02x", $0) }
      .joined()
    
    return hashString
  }
  
  /// 16位：32 位中间的 16 位
  public static func MD5(_ input: String) -> String {
    let inputData = Data(input.utf8)
    let hashedData = Insecure.MD5.hash(data: inputData)
    
    let hashString = hashedData
      .compactMap { String(format: "%02x", $0) }
      .joined()
    
    return hashString
  }
}
